/* 
 * Copyright (c) 2010 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.enterprise.gsa;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author todd.re...@gmail.com (Todd)
 *
 */
public class PromotedResult {
  /* The query that would trigger displaying this promoted result */
  private String query;
  /* The URL to which this promoted result points */
  private String url;  
  /* The descriptive title of this promoted result */
  private String title;
  
  /**
   * Creates a PromotedResult object with the given information
   * 
   * @param query The query that triggers this promoted result
   * @param url The URL that this promoted result points to
   * @param title The descriptive title of this promoted result
   */
  public PromotedResult(String query, String url, String title) {
    this.query = query;
    this.url = url;
    this.title = title;
  }
    
  /**
   * Attempts to give a helpful string based on the URL status.
   * 
   * @return a {@code String} that describes the HTTP return code.
   */
  public String getUrlStatusString() {    
    URL myURL;
    String responseMsg = "Server Unreachable";
    try {
      myURL = new URL(url);
    } catch (MalformedURLException ex) {
      return responseMsg;
    }
           
    try {
      HttpURLConnection conn = (HttpURLConnection) myURL.openConnection();
      conn.setRequestMethod("HEAD");
      conn.setInstanceFollowRedirects(true);
      conn.connect();
      responseMsg = conn.getResponseMessage();
    } catch (IOException e) {
      return responseMsg;
    }    
    return responseMsg;
  }

  /**
   * 
   * @return the query string that triggers this promoted result
   */
  public String getQuery() {
    return query;
  }

  /**
   * Sets the trigger query for this promoted result
   * @param query the new query string
   */
  public void setQuery(String query) {
    this.query = query;
  }

  /**
   * 
   * @return the URL that this promoted points to
   */
  public String getUrl() {
    return url;
  }

  /**
   * Set the URL that this promoted result will point to
   * @param url the new URL to point at
   */
  public void setUrl(String url) {
    this.url = url;
  }
  
  /**
   * 
   * @return the descriptive title of this promoted result
   */
  public String getTitle() {
    return title;
  }

  /**
   * Sets the descriptive title
   * 
   * @param title the new descriptive title
   */
  public void setTitle(String title) {
    this.title = title;
  }
}
